<?php

/***
 * 描述：团购模型
 * 
 * @category		Lib
 * @package			Model			
 * @subpackage	
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-7-30
 * @version			$Id: TuangouModel.class.php 92 2013-12-12 15:45:09Z whtaok@gmail.com $
 */
class TuangouModel {
	/**
	 * 描述：查询团购商品
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		array 	$search		查询条件name
	 * @param 		string 	$order		排序
	 * @since		2013-4-21
	 */
	public static function findListPage($search,$order='ordlist'){	
		$modWhere = array();
		if($search['name']){
			$modWhere['name']=array('like','%'.$search['name'].'%');
		}else if($search['cate']){
			$modWhere['cate_code2']=array('like','%'.$search['cate'].'%');
		}
		$modWhere['is_show'] = 1;
		$mod = new CommonModel('TgProdLib');
		//查询总数
		$totalRows = $mod->setModWhere($modWhere)->count();
		//查询分页
		list($list,$page)= $mod->field('id as prod_id,name,title,spec,spec_qty,spec_unit,price_real,price_sale,is_hot,is_show')->setModWhere($modWhere)->order('is_hot desc,ordlist')->listPage($totalRows);
		foreach ($list as $key => $value){
			$list[$key]['prod_pic']=m_prod_pic($value['prod_id'],'tg_prod');
			$list[$key]['prod_spec']=m_prod_spec($value['spec'],$value['spec_qty'],$value['spec_unit']);
		}
		return array($list,$page);
	}
	
	/**
	 * 描述：查询详细
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		array 	$search		查询条件name
	 * @param 		string 	$order		排序
	 * @since		2013-4-21
	 */
	public static function findDetail($id){
		$mod = new CommonModel('TgProdLib');
		$detail = $mod->field('id as prod_id,cate_code2,cate_code1,name,title,spec,spec_qty,spec_unit,price_real,price_sale,is_hot,is_show')->find($id);
		$detail['prod_pic']=m_prod_pic($detail['prod_id'],'tg_prod','big');
		$detail['prod_spec']=m_prod_spec($detail['spec'],$detail['spec_qty'],$detail['spec_unit']);
		if($detail){
			$detail['is_exist'] = 1;
		}else{
			$detail['is_exist'] = 0;
		}
		return $detail;
	}
	
	public static function findDetailContent($id){
		
		if(defined('IS_MOBILE') && IS_MOBILE){
			$field = 'content';
		}else{
			$field = 'content_pc';
		}
		$mod = new CommonModel('TgProdLib');
		$detail = $mod->field($field)->find($id);
		
		if($detail){
			$rtnData = array(
					'error'=>0,
					'message'=>'',
					'content'=>$detail[$field],
			);
		}else{
			$rtnData = array(
					'error'=>1,
					'message'=>'加载详细时发生错误',
			);
		}
		return $rtnData;
	}
	
	/**
	 * 描述：添加商品数量
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @param 		string 	$prod_id		商品ID
	 * @param 		string 	$num			购买数量
	 * @since		2013-4-21
	 */
	public static function updBuyNum($prod_id,$num){
		$mod = new CommonModel('TgProdLib');
		$modWhere['is_show'] = 1;
		$modWhere['id'] = $prod_id;
		$detail = $mod->where($modWhere)->find();
		if($detail){
			$maxNum = C('BUY_MAX_NUM');
			if($num>$maxNum){
				$totalAmt = $maxNum * $detail['price_real'];
				$rtnData = array(
						'error'=>'0',
						'message'=>'该商品最多可购买【'.$maxNum.'】件',
						'total_amt' => number_format($totalAmt,2),
						'item_num' => $maxNum
				);
			}else{
				$totalAmt = $num * $detail['price_real'];
				$rtnData = array(
						'error'=>'0',
						'message'=>'',
						'total_amt' => number_format($totalAmt,2),
						'item_num' => $num
				);
			}
			
		}else{
			$rtnData = array(
					'error'=>'1',
					'message'=>'商品已下架',
					'total_amt' => 0.00,
					'item_num' => $num
			);
		}
		return $rtnData;
	}

	
}

?>